<?php
namespace app\index\controller;

use app\common\controller\Base;
use think\Db;
use GuzzleHttp\json_encode;

class Agent extends Base
{

    public function _initialize()
    {
        parent::_initialize();
        $start = array(
            'user_all' => $this->user_all,
            'logo' => $this->logo,
            'app_down' => $this->app_down,
            'now_time_windows' => $this->now_time_windows,
            'usnameclass' => $this->usnameclass
        );
        $res = Db::name('username')->where('username', cookie('tjrname'))->where('dl', '>', '0')->find();
        $this->assign('user', cookie('tjrname'));
        $this->assign('r', $res);
        $this->assign($start);
    }

    public function login()
    {
        $username = input('post.username', '');
        $password = input('post.password', '');
        if ($username && $password) {
            $where = array('username' => $username, 'password' => md5($password), 'dl' => array('>', 0));
            $res = Db::name('username')->where($where)->find();
            if ($res['id']) {
                cookie('tjrname', $username);
                cookie('dlpassword', $res['password']);
                $login_key = suiji_zifu(15);
                cookie('login_key', $login_key);
                $this->caozuoRizhiJilu(2, '金字塔代理已登录', get_ip(), $username);

                $a = model('Username')->updateLoginInfo($res['id'], get_ip(), time(), $res['us_class']);
                return 'ok';
            } else {
                return 'no';
            }
        }
        return $this->fetch();
    }

    public function index()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号!', url('agent/login'));
        }
        return $this->fetch('agent/index');

    }

    public function dai()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号!', url('agent/login'));
        }
        $res = Db::name('username')->where('username', cookie('tjrname'))->find();
        $chakid_id = input('chakid_id', '');
        $ctixian_id = input('ctixian_id', '');
        $ctouzhu_id = input('ctouzhu_id', '');
        $cdefdid = input('cdefdid', '');
        $fandian = input('fandian', '');
        $keywords = input('post.keywords', '');
        $page = input('get.page', '');

        $page_num = 10;
        //会员充值信息
        if ($chakid_id) {
            $dlchakanid = Db::name('username')->where(['id' => $chakid_id, 'tjr' => $res['username']])->find();
            if ($dlchakanid) {
                $count = Db::name('chongzhi_list')->where('username', $dlchakanid['username'])->count();
                //计算总页数
                $zys = ceil($count / $page_num);
                //调整页码
                if ($page > $zys) $page = $zys;
                if ($page < 1) $page = 1;
                //查询会员信息
                $offset = ($page - 1) * $page_num;
                $xx_listdb = Db::name('chongzhi_list')->where('username', $dlchakanid['username'])->order('id', 'DESC')->limit($offset, $page_num)->select();
                foreach ($xx_listdb as &$rt) {
                    $rt['cztime'] = date('Y-m-d H:i:s', $rt['cztime']);
                    if ($rt['zt'] == 1) {
                        $rt['zt'] = '已到账';
                    } else {
                        $rt['zt'] = '处理中';
                    }
                }
            }
            $this->assign('current', 'agent');
            $this->assign('zys', $zys);
            $this->assign('count', $count);
            $this->assign('page', $page);
            $this->assign('chakid_id', $chakid_id);
            $this->assign('touzhulist', $xx_listdb);
            return $this->fetch('daili1');
        }
        //会员提现信息
        if ($ctixian_id) {
            $dlchakanid = Db::name('username')->where(['id' => $ctixian_id, 'tjr' => $res['username']])->find();
            if ($dlchakanid) {
                $count = Db::name('tixian_list')->where('username', $dlchakanid['username'])->count();
                //计算总页数
                $zys = ceil($count / $page_num);
                //调整页码
                if ($page > $zys) $page = $zys;
                if ($page < 1) $page = 1;
                //查询会员信息
                $offset = ($page - 1) * $page_num;
                $xx_listdb = Db::name('tixian_list')->where('username', $dlchakanid['username'])->order('id', 'DESC')->limit($offset, $page_num)->select();
                foreach ($xx_listdb as &$rt) {
                    $rt['txtime'] = date('Y-m-d H:i:s', $rt['txtime']);
                    if ($rt['zt'] == 0) $rt['zt'] = '未处理';
                    if ($rt['zt'] == 1) $rt['zt'] = '已处理';
                    if ($rt['zt'] == 2) $rt['zt'] = '退回';
                }
            }
            $this->assign('current', 'agent');
            $this->assign('zys', $zys);
            $this->assign('count', $count);
            $this->assign('page', $page);
            $this->assign('ctixian_id', $ctixian_id);
            $this->assign('touzhulist', $xx_listdb);
            return $this->fetch('daili2');
        }
        //会员投注信息
        if ($ctouzhu_id) {
            $dlchakanid = Db::name('username')->where(['id' => $ctouzhu_id, 'tjr' => $res['username']])->find();
            if ($dlchakanid) {
                $count = Db::name('touzhu_list_ok')->where('username', $dlchakanid['username'])->count();
                //计算总页数
                $zys = ceil($count / $page_num);
                //调整页码
                if ($page > $zys) $page = $zys;
                if ($page < 1) $page = 1;
                //查询会员信息
                $offset = ($page - 1) * $page_num;
                $xx_listdb = Db::name('touzhu_list_ok')->alias('tlo')->join('__CAIPAI_LIST__ cl', 'cl.id=tlo.czid')->field('tlo.*,cl.cp_name_new,cl.cp_name_old')->where('tlo.username', $dlchakanid['username'])->order('tlo.id', 'DESC')->limit($offset, $page_num)->select();
                foreach ($xx_listdb as &$rt) {
                    $rt['tz_time'] = date('Y-m-d H:i:s', $rt['tz_time']);
                    if (!$rt['cp_name_new']) $rt['cp_name_new'] = $rt['cp_name_old'];
                }
            }
            $this->assign('current', 'agent');
            $this->assign('zys', $zys);
            $this->assign('count', $count);
            $this->assign('page', $page);
            $this->assign('ctouzhu_id', $ctouzhu_id);
            $this->assign('touzhulist', $xx_listdb);
            return $this->fetch('daili3');
        }
        //下线会员首页
        if ($keywords) {
            $count = Db::name('username')->where('tjr', $res['username'])->where('username', 'like', '%' . $keywords . '%')->where('dl', '<', 1)->count();
        } else {
            $count = Db::name('username')->where('tjr', $res['username'])->where('dl', '<', 1)->count();
        }
        //计算总页数
        $zys = ceil($count / $page_num);
        //调整页码
        if ($page > $zys) $page = $zys;
        if ($page < 1) $page = 1;
        //查询会员信息
        $offset = ($page - 1) * $page_num;
        if ($keywords) {
            $xx_listdb = Db::name('username')->where('tjr', $res['username'])->where('username', 'like', '%' . $keywords . '%')->where('dl', '<', 1)->order('id', 'DESC')->limit($offset, $page_num)->select();
        } else {
            $xx_listdb = Db::name('username')->where('tjr', $res['username'])->where('dl', '<', 1)->order('id', 'DESC')->limit($offset, $page_num)->select();
        }
        //处理时间
        $zsr = 0;
        foreach ($xx_listdb as &$rt) {
            $rt['reg_time'] = date('Y-m-d H:i:s', $rt['reg_time']);
            $rt['sr'] = $rt['tzzje'] * $res['fandian'] / 100;
            $zsr += $rt['sr'];
        }
        $this->assign('current', 'agent');
        $this->assign('zys', $zys);
        $this->assign('zsr', $zsr);
        $this->assign('count', $count);
        $this->assign('page', $page);
        $this->assign('touzhulist', $xx_listdb);
        $this->assign('tjm', $res['tjm']);
        return $this->fetch();
    }

    /**
     * @Purpose 代理管理(下线代理)
     * @Time 2017-12-02
     */
    public function branchAgent()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号!', url('agent/login'));
        }
        $keywords = input('post.keywords') ? input('post.keywords') : '';
        $page = input('get.page') ? input('get.page') : 1;
        $page_num = 10;
        $zsr = 0;
        $res = Db::name('username')->where('username', cookie('tjrname'))->find();
        //判断是否带搜索条件
        if ($keywords) {
            $count = Db::name('username')->where('tjr', $res['username'])->where('username', 'like', '%' . $keywords . '%')->where('dl', '>', 0)->count();
        } else {
            $count = Db::name('username')->where('tjr', $res['username'])->where('dl', '>', 0)->count();
        }
        //计算总页数
        $zys = ceil($count / $page_num);
        //调整页码
        if ($page > $zys) $page = $zys;
        if ($page < 1) $page = 1;
        //查询代理会员信息
        $offset = ($page - 1) * $page_num;
        if ($keywords) {
            $xx_listdb = Db::name('username')->field('id,username,reg_time,fandian,isok')->where('tjr', $res['username'])->where('username', 'like', '%' . $keywords . '%')->where('dl', '>', 0)->order('id', 'DESC')->limit($offset, $page_num)->select();
        } else {
            $xx_listdb = Db::name('username')->field('id,username,reg_time,fandian,isok')->where('tjr', $res['username'])->where('dl', '>', 0)->order('id', 'DESC')->limit($offset, $page_num)->select();
        }

        //处理时间
        $users = '';
        foreach ($xx_listdb as &$rt) {
            $rt['reg_time'] = date('Y-m-d H:i:s', $rt['reg_time']);
            $users .= $rt['username'] . ',';
            $rt['member'] = $this->huiyuanrenshu($rt['username']);
            $rt['agent'] = $this->dailirenshu($rt['username']);

        }
        //处理代理团队总和信息
        $users = trim($users, ',');
        $sums = Db::name('username')->field('tjr,COUNT(*) as count,SUM(czzje) as czzjes,SUM(txzje) as txzjes,SUM(zyh_tzzje) as tzzjes,fandian')->where('tjr', 'in', $users)->group('tjr,fandian')->select();

        //汇总
        foreach ($xx_listdb as $k => &$v) {
            $v['t'] = 0;
            $v['sr'] = 0;
            foreach ($sums as $ss) {
                if ($ss['tjr'] == $v['username']) {
                    $v['count'] = $this->membershu($v['username']);
                    $v['count'] = $this->membershu($v['username']) - $v['count'];
                    $v['tzzjes'] = $this->tdzongtz($v['username']);
                    $v['tzzjes'] = $this->tdzongtz($v['username']) - $v['tzzjes'];
                    $v['yl'] = round($v['tzzjes'] * $v['fandian'] / 100, 2);
                    $v['sr'] = round($v['tzzjes'] * ($res['fandian'] - $v['fandian']) / 100, 2);
                    $s = $this->tdzongyl($v['username']);
                    $v['t'] = ($v['tzzjes'] - $s) * $v['fandian'] / 100 + $s * ($v['fandian'] - $ss['fandian']) / 100;

                    $zsr += $v['sr'];
                    $v['czzjes'] = $this->tdzongcz($v['username']);
                    $v['czzjes'] = $this->tdzongcz($v['username']) - $v['czzjes'];
                }
            }
        }
        $arr = Db::name('username')->select();
        $t = $this->szt($arr);
        $this->assign('current', 'agent');
        $this->assign('zys', $zys);
        $this->assign('count', $count);
        $this->assign('page', $page);
        $this->assign('zsr', $zsr);
        $this->assign('touzhulist', $xx_listdb);
        $this->assign('tjm', $res['tjm']);
        return $this->fetch('agent/dl');
    }

    //会员人数
    public function huiyuanrenshu($user)
    {
        $rs = Db::name('username')->where(['tjr' => $user, 'dl' => ['<', 1]])->select();
        $cont = count($rs);
        return $cont;
    }

    //代理人数
    public function dailirenshu($user)
    {
        $rs = Db::name('username')->where(['tjr' => $user, 'dl' => ['>', 0]])->select();
        $cont = count($rs);
        return $cont;
    }

    //团队总盈利
    public function tdzongyl($user)
    {
        $rs = Db::name('username')->where(['tjr' => $user])->select();

        static $m = 0;
        foreach ($rs as $v) {
            if ($v['dl'] < 1) {

                //如果会员是代理
            } else {
                $r = Db::name('username')->where(['tjr' => $user, 'dl' => ['>', 0]])->select();
                //如果会员是代理，取出会员的会员
                if (is_array($r)) {
                    foreach ($r as $s) {
                        $rs = Db::name('username')->where(['tjr' => $s['username']])->select();
                        foreach ($rs as $v) {
                            //如果会员不是代理
                            if ($v['dl'] < 1) {
                                $m += $v['tzzje'];

                                //如果会员是代理
                            } else {
                                $r = Db::name('username')->where(['tjr' => $user, 'dl' => ['>', 0]])->select();
                                if (is_array($r)) {
                                    foreach ($r as $b) {
                                        $this->tdzongyl($b['username']);
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }

        return $m;
    }

    //团队投注总金额
    public function tdzongtz($user)
    {
        $rs = Db::name('username')->where(['tjr' => $user])->select();

        static $c = 0;
        foreach ($rs as $v) {
            if ($v['dl'] < 1) {
                $c += $v['tzzje'];
            } else {
                $r = Db::name('username')->where(['tjr' => $user, 'dl' => ['>', 0]])->select();
                if (is_array($r)) {
                    foreach ($r as $s) {

                        $this->tdzongtz($s['username']);
                    }
                }
            }
        }
        return $c;
    }

    //团队总充值
    public function tdzongcz($user)
    {
        $rs = Db::name('username')->where(['tjr' => $user])->select();

        static $b = 0;
        foreach ($rs as $v) {
            if ($v['dl'] < 1) {
                $b += $v['czzje'];
            } else {
                $r = Db::name('username')->where(['tjr' => $user, 'dl' => ['>', 0]])->select();
                if (is_array($r)) {
                    foreach ($r as $s) {

                        $this->tdzongcz($s['username']);
                    }
                }
            }
        }
        return $b;
    }

    //团队会员总人数
    public function membershu($user)
    {
        $rs = Db::name('username')->where(['tjr' => $user])->select();

        static $a = 0;
        foreach ($rs as $v) {
            if ($v['dl'] < 1) {
                $a = $a + 1;
            } else {
                $r = Db::name('username')->where(['tjr' => $user, 'dl' => ['>', 0]])->select();
                if (is_array($r)) {
                    foreach ($r as $s) {

                        $this->membershu($s['username']);
                    }
                }
            }
        }
        return $a;
    }

    //当达到代理设置最高层级时，无法发展下级代理
    public function szt($arr, $pid = 0, $lev = 0)
    {
        $xt = Db::name('xt_dl')->where('id', 1)->find();
        static $cate_arr = array();
        foreach ($arr as $v) {
            if ($v['tjrid'] == $pid) {
                if ($lev == $xt['max']) {
                    $data = array('isok' => 0);
                    Db::name('username')->where('username', $v['username'])->update($data);
                }
                $this->szt($arr, $v['id'], $lev + 1);
            }
        }
    }

    /**
     * @Purpose 代理管理(添加代理)
     * @Time 2017-12-04
     */
    public function addAgent()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号!', url('agent/login'));
        }
        $tj = input('tj', '');
        $username = input('post.zhanghao1', '');
        $password = input('post.password1', '');
        $fandian = input('post.return0', '');
        $isok = input('post.agentUnder', '');
        $res = Db::name('username')->where('username', cookie('tjrname'))->find();

        if ($tj == 'yes') {
            if ($res['isok'] == 1) {
                if ($username && $password && $fandian) {
                    $ntime = time();
                    $password = md5($password);
                    $ifcss = Db::name('username')->where('username', $username)->find();
                    if ($ifcss) {
                        echo "no";
                        exit;
                    }

                    if ($fandian >= $res['fandian']) {
                        echo "no";
                        exit;
                    }
                    $tjm = suiji_zifu(13);
                    //解析IP
                    $ip = @file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$this->login_ip}");
                    if ($ip) {
                        $ip = json_decode($ip, true);
                        $position = $ip['country'] . ' ' . $ip['province'] . ' ' . $ip['city'];
                    }

                    //注册下线代理
                    model('Username')->setNewUser($res['id'], 0, $username, $password, $res['username'], 0, $ntime, $this->login_ip, $position, 1, '', $fandian, 1, '', 1, $tjm, $isok);
                    echo 'ok';
                } else {
                    echo "no";
                }
            } else {
                echo "no";
            }
        }

    }

    /**
     * @Purpose 代理管理(添加会员)
     * @Time 2017-12-04
     */
    public function addMember()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号!', url('agent/login'));
        }
        $tj = input('tj') ? input('tj') : '';
        $username = input('post.zhanghao2') ? input('post.zhanghao2') : '';
        $password = input('post.password2') ? input('post.password2') : '';
        $res = Db::name('username')->where('username', cookie('tjrname'))->find();
        if ($tj == 'yes') {
            if ($username && $password) {
                //解析IP
                $ip = @file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$this->login_ip}");
                if ($ip) {
                    $ip = json_decode($ip, true);
                    $position = $ip['country'] . ' ' . $ip['province'] . ' ' . $ip['city'];
                }
                $ntime = time();
                $password = md5($password);
                $ifcss = Db::name('username')->where('username', $username)->find();
                if ($ifcss) {
                    echo "no";
                    exit;
                }
                //注册下线会员
                model('Username')->setNewUser($res['id'], 0, $username, $password, $res['username'], 0, $ntime, $this->login_ip, $position, 1, '', 0, 1, '', 0, '');
                echo "ok";
            } else {
                echo "no";
            }
        }

    }

    /**
     * @Purpose 电脑版用户消息中心
     * @Time 2017-11-18
     */
    public function message()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号！', url('agent/login'));
            exit;
        }
        //消息列表
        $xx_listdb = Db::name('xiaoxi')->where('username', cookie('tjrname'))->order('id', 'DESC')->limit(100)->select();

        $this->assign('xiaoxi_ls', $xx_listdb);
        return $this->fetch();
    }

    /**
     * @Purpose 电脑版提现记录
     * @Time 2017-11-18
     */
    public function drawMoneyRecord()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号！', url('agent/login'));
            exit;
        }
        $page = input('page', 1);
        $page_num = 10;
        $res = Db::name('username')->where('username', cookie('tjrname'))->find();
        //查询提现记录
        $tixianjl = $this->tixianjl($res['username']);
        //总页数
        $zcount = count($tixianjl);
        $zys = ceil($zcount / $page_num);
        //页数限制
        if ($page > $zys) $page = $zys;
        if ($page < 1) $page = 1;
        $offset = ($page - 1) * $page_num;
        //处理提现记录
        if (count($tixianjl) > 0) {
            $tixianjl = multi_array_sort($tixianjl, 'txtime');
        }
        $tixianjl = array_slice($tixianjl, $offset, $page_num);
        foreach ($tixianjl as &$xh_1) {
            if ($xh_1['zt'] == '1') $xh_1['zt'] = '成功';
            if ($xh_1['zt'] == '0') $xh_1['zt'] = '处理中';
            $xh_1['txtime'] = date('Y-m-d H:i:s', $xh_1['txtime']);
        }
        $bank_list = Db::name('bank')->select();
        $time = strtotime('Y-m-d', time());
        $star = strtotime($time . ' 00:00:00');
        $end = strtotime($time . ' 23:59:59');
        $je = Db::name('tixian_list')->where(['username' => cookie('tjrname'), 'txtime' => ['between', $star . ',' . $end]])->select();
        $tx = 0;
        $cs = 0;
        foreach ($je as $v) {
            $tx += $v['txje'];
            $cs = $cs + 1;
        }
        $draw_set = Db::name('xt_tixian')->find();
        $this->assign('draw', $draw_set);
        $this->assign('tx', $tx);
        $this->assign('cs', $cs);
        $this->assign('bank', $bank_list);
        $this->assign('current', 'drawMoney');
        $this->assign('zys', $zys);
        $this->assign('res', $res);
        $this->assign('zcount', $zcount);
        $this->assign('page', $page);
        $this->assign('touzhulist', $tixianjl);
        return $this->fetch('caiwu');
    }

    /**
     * @Purpose 电脑版用户修改密码
     * @Time 2017-11-17
     */
    public function updatePass()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号！', url('agent/login'));
            exit;
        }
        $this->assign('current', 'accountInfo');
        return $this->fetch('password');
    }

    /**
     * @Purpose 代理提现
     * @Time 2017-12-01
     */
    public function withdrawDeposit()
    {
        //开启事务
        Db::startTrans();
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号！', url('agent/login'));
            exit;
        }
        $amount = input('post.amount') ? floatval(input('post.amount')) : '';
        $withdrawal_cipher = input('post.funds_password','');
        $d = Db::name('username')->where('username', cookie('tjrname'))->find();
        if ($amount && $withdrawal_cipher) {
            //验证二级密码是否正确
            $withdrawal_cipher = md5($withdrawal_cipher);
            if ($withdrawal_cipher == $d['ejmm']) {

                //提现设定处理
                $draw_set = Db::name('xt_tixian')->find();
                if ($draw_set['open'] < 1) {
                    js_tishi('提现功能尚未开启！', '/index/agent/dai');
                    exit;
                }
                //提现次数判断
                $now_times = date('Y-m-d') . " 00:00:00";
                $now_times = strtotime($now_times);
                $count = Db::name('tixian_list')->where('username', $d['username'])->where('txtime', '>', $now_times)->count();
                if ($draw_set['txcount'] > 0) {
                    if ($count >= $draw_set['txcount']) {
                        js_tishi('今日提现次数已用完！', '/index/agent/dai');
                        exit;
                    }
                }
                //判断账号是否为冻结或者提现功能是否关闭或者为试玩账号
                if ($d['open'] == 1 || $d['swzh'] == 1 || $d['tixian_open'] == 1) {
                    js_tishi('您的账号目前为禁止提现状态！', '/index/agent/dai');
                    exit;
                }
                //判断提现金额是否满足设置金额
                if ($amount < $draw_set['zxje'] || $amount > $draw_set['zdje']) {
                    js_tishi("提现金额不能小于{$draw_set['zxje']}，且不能大于{$draw_set['zdje']}", '/index/agent/dai');
                    exit;
                }
                if ($amount > $d['je']) {
                    js_tishi('您账号金额不足！', '/index/agent/dai');
                    exit;
                }
                if ($d['nbzh'] == 1) {
                    $ztin = '1';
                    $caadmin = '系统';
                } else {
                    $ztin = '0';
                    $caadmin = '';
                }
                try {
                    //插入提现信息
                    model('TixianList')->setDrawInfo($d['username'], $amount, $ztin, time(), $this->login_ip, $caadmin);
                    //更新用户金额
                    model('Username')->updateMoneyInfo($d['username'], $amount, 'down');
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return js_tishi('提现失败请重试！', '/index/agent/dai');
                }
                return js_tishi('提现成功，等待审核中！', '/index/agent/dai');
            } else {
                return js_tishi('提款密码错误！', '/index/agent/dai');
            }
        } else {
            return js_tishi('资料填写不完整！', '/index/agent/dai');
        }
    }

    /**
     *
     * @Purpose 代理绑定提现银行卡
     * @Time 2017-12-14
     */
    public function bankBind()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号！', url('agent/login'));
            exit;
        }
        $account_holder = input('post.bankren', '');
        $account_bank = input('post.bankname', 0);
        $account_number = input('post.bankno', '');
        $province = input('post.banksf', '');
        $city = input('post.banksq', '');
        $mobile = input('post.mobile', '');
        $qq = input('post.qq', '');
        $withdrawal_cipher = input('post.ejmm', '');
        $res = Db::name('username')->where('username', cookie('tjrname'))->find();
        if ($account_holder && $account_bank && $account_number && $province && $city && $mobile && $qq && $withdrawal_cipher) {
            $withdrawal_cipher = md5($withdrawal_cipher);
            //绑定用户银行信息
            if ($res['id']) {
                model('Username')->bindBankInfo($res['username'], $account_holder, $account_bank, $account_number, $province, $city, $mobile, $qq, $withdrawal_cipher);
                echo 'ok';
            } else {
                echo 'no';
            }
        } else {
            echo 'no';
        }
    }

    /**
     * @Purpose 用户修改密码
     * @Time 2017-11-17
     */
    public function updatePassd()
    {
        if (!cookie('tjrname')) {
            js_tishi('请先登录账号！', url('agent/login'));
            exit;
        }
        $old_password = input('post.oldPass', '');
        $new_password = input('post.newPass', '');
        $new_password2 = input('post.oldPasse', '');
        $passwordclass = input('post.way', '');
        //验证密码信息
        if ($old_password && $new_password && $new_password2 && $passwordclass) {
            if ($passwordclass == '1') {
                $old_password = md5($old_password);
                if ($old_password == cookie('dlpassword')) {
                    if ($new_password == $new_password2) {
                        $new_password = md5($new_password);
                        //记录日志并修改密码
                        $this->caozuoRizhiJilu('2', '修改了登录密码', $this->login_ip, cookie('tjrname'));
                        model('Username')->updatePass(cookie('tjrname'), $new_password);
                        cookie('tjrname', null);
                        cookie('dlpassword', null);
                        cookie('login_key', null);
                        echo 'ok';
                    } else {
                        echo 'no1';
                    }
                } else {
                    echo 'no2';
                }
            } else {
                echo 'no3';
            }
        } else {
            echo 'no4';
        }
    }

    public function logout()
    {
        cookie('tjrname', null);
        cookie('dlpassword', null);
        cookie(null);
        $this->redirect('index/agent/login');
    }
    public function hezuoshang(){
    	return $this->fetch();
    }
    
    
}